<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Component
 * @version $Id$
 */

/**	
 * A Component Configuration Parameter
 *	
 * This class is a simple container that is used to store component configuration parameters.
 * Following parameter types are supported currently:
 * <ul>
 *   <li>
 *     Boolean
 *     Integer
 *     Float
 *     String
 * 	 </li>
 *   <li>
 *     class reference
 *     component reference
 *   </li>
 *   <li>
 *     hash
 *     sets
 *     array
 *   </li>     
 * </ul>
 * 
 * @package auxo
 * @subpackage Component
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_ComponentConfigurationParameter {
	/**
	 * All kinds of simple parameter types
	 *
	 */
	const TYPE_BOOLEAN = 'boolean';
	const TYPE_INTEGER = 'int';
	const TYPE_STRING = 'string';
	const TYPE_FLOAT = 'float';
	
	/**
	 * all kind of reference types that are supported
	 *
	 */
	const TYPE_CLASSREF = 'classref';
	const TYPE_REF = 'ref';
	
	/**
	 * all kind of array types that are supported
	 *
	 */
	const TYPE_HASH = 'hash';
	const TYPE_SET = 'set';
	const TYPE_ARRAY = 'array';
	
	/**
	 * Parameter Name
	 * 
	 * @var string
	 */
	private $name = '';
	
	/**
	 * Creates an new parameter
	 *
	 * @param string $type
	 * @param mixed $data
	 * @see tx_auxo_ComponentConfigurationParameter
	 */
	public function __construct($type=self::TYPE_STRING, $data='') {
		$this->type = $type;
		$this->data = $data;
	}
	
	/**
	 * Sets parameter name
	 *
	 * @param string $name name of parameter
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * Returns parameter name
	 *
	 * @return string name of parameter
	 */
	public function getName() {
		return $this->name;
	}
	
	/**
	 * Sets/overwrites type and data 
	 *
	 * @param string $type
	 * @param mixed $data
	 * @see tx_auxo_ComponentConfigurationParameter 
	 */
	public function set($type, $data) {
		$this->type = $type;
		$this->data = $data;		
	}
	
	/**
	 * Sets data of this parameter
	 *
	 * @param mixed $data
	 */
	public function setData($data) {
		$this->data = $data;
	}

	/**
	 * Convertes a string into typed data 
	 * 
	 * Following data types are detected automatically:
	 * boolean, integer, float and string.
	 *
	 * @param string $value value as string
	 */	
	public function mapValueToData($value) {
		$value = trim($value);
		
		if (strtolower($value) == 'true' or strtolower($value) == 'false') {
			$flag = strtolower($value) == 'true' ? true : false;
			$this->set(self::TYPE_BOOLEAN, $flag);
		}
		elseif (is_numeric($value)) {
			if (strpos($value, '.')) {
				$this->set(self::TYPE_FLOAT, (float) $value);
				
			}
			else {
				$this->set(self::TYPE_INTEGER, (int) $value);
			}
		}
		else {		
			$this->set(self::TYPE_STRING, (string) $value);		
		}
	}
	
	/**
	 * Returns data of this parameter
	 *
	 * @return mixed
	 */
	public function getData() {
		return $this->data;
	}
	
	/**
	 * Sets type of this parameter
	 *
	 * @param string $type
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * Returns type of this parameter
	 *
	 * @return string
	 * @see tx_auxo_ComponentConfigurationParameter
	 */
	public function getType() {
		return $this->type;
	}
}
?>